package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.file.FileUploadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@SaIgnore
@RequiredArgsConstructor
@RestController
public class CommonController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

	@Value("${ruoyi.imgUrl}")
    private String imgUrl;
    @Value("${ruoyi.profile}")
    private String profile;

	/**
     * 通用上传请求
     */
    @PostMapping("/common/upload/{uploadType}")
    public R uploadFile(@PathVariable(value = "uploadType") Integer uploadType, MultipartFile file) throws Exception {
        try {
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(profile, file, uploadType);
            //图片网络路径
            String url = imgUrl + fileName;
            Map<String,Object> ajax = new HashMap<>();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return R.ok(ajax);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
}
